<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <form class="layui-form">
                <div class="layui-form lay-header">
                    <div class="toolbar">
                        <div class="layui-form-item lay-query-content">
                            <div class="lay-query-box">
                                <div class="query-lt">
                                    <div class="layui-inline">
                                        <label class="layui-form-label w-auto">物料编号：</label>
                                        <div class="layui-input-inline mr0">
                                            <input name="skuCode" class="layui-input" type="text" placeholder="物料编号" autocomplete="off"/>
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label w-auto">仓库：</label>
                                        <div class="layui-input-inline mr0">
                                            <select name="warehouseId" class="common-select-search"></select>
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label w-auto">库存变动时间：</label>
                                        <div class="layui-input-inline">
                                            <input  id="pTime" class="layui-input date-icon" type="text" placeholder="选择库存变动时间" autocomplete="off" readonly/>
                                            <input name="startCreateTime" id="sTime" style="display: none"/>
                                            <input name="endCreateTime" id="eTime" style="display: none"/>
                                        </div>
                                    </div>
                                </div>
                                <div class="query-rt">
                                    <div class="layui-inline">
                                        <button class="layui-btn icon-btn" lay-filter="formSubSearchLog" lay-submit
                                                type="button" layui-form-keyDownSearch="true">查询
                                        </button>
                                        <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit
                                                type="reset">
                                            重置
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="lay-btn-rows">
                        <div class="lay-btn-box">
                            <div class="lay-btn-lt">
                                <div id="inventoryProductRecordOutExport" class="layui-btn icon-btn color-green">导出</div>
                                <!--<div id="sendMessage" class="layui-btn icon-btn color-green">发送消息</div>-->
                            </div>
                            <div class="lay-btn-rt">

                            </div>
                        </div>
                    </div>
                </div>
            </form>
            <!-- 表格 -->
            <table class="layui-table" id="inventoryProductRecordOutTable" lay-filter="inventoryProductRecordOutTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="inventoryProductRecordOutTableBar">
    <a class="layui-btn  layui-btn-xs" lay-event="details">详情</a>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="inventoryProductRecordOutForm">
    <div class="layui-card">
        <div class="layui-card-body">
            <form lay-filter="inventoryProductRecordOutForm" class="layui-form model-form">
                <div class="layui-form-item">
                    <table class="layui-table" id="detailsTable" lay-filter="detailsTable"></table>
                </div>
                <div class="layui-form-item">
                    <table class="layui-table" id="detailsTableList" lay-filter="detailsTableList"></table>
                </div>
            </form>
        </div>
    </div>
</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin','laydate'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;
        var laydate = layui.laydate;

        admin.selectSearch();

         //时间范围
         laydate.render({
            elem: '#pTime',
            type: 'date',
            range: true,
            done: function(value, date, endDate){
                if (value){
                    let dateValue = value.split(" - ");
                    $("input[name='startCreateTime']").val(dateValue[0] + " 00:00:00");
                    $("input[name='endCreateTime']").val(dateValue[1] + " 23:59:59");
                }else {
                    $("input[name='startCreateTime']").val("");
                    $("input[name='endCreateTime']").val("");
                }
                setTimeout(function() {$('[lay-filter="formSubSearchLog"]').click()}, 300);
            }
        });

        // 获取仓库
        admin.req('enterprisecab/warehouse/admin/warehouse/list', {limit:9999}, function (res) {
            if (200 == res.code) {
                var optionHtml='<option value="">请选择仓库</option>';
                for(var k=0;k<res.data.length;k++){
                    optionHtml+='<option value="'+res.data[k].id+'">'+res.data[k].name+'</option>';
                }
                $("select[name='warehouseId']").html(optionHtml);
                form.render('select');
            } else {
                layer.msg('获取仓库失败', {icon: 2, time: 1000});
            }
        }, 'GET');

        // 渲染表格
        var insTb = table.render({
            elem: '#inventoryProductRecordOutTable',
            url: config.base + 'enterprisecab/order/admin/inventoryProductRecordOut/list',
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'numbers',title: '序号', fixed: 'left'}
                ,{field: 'skuName', title: '物料名称', fixed: 'left', width: 160}
                ,{field: 'skuCode', title: '物料编号', fixed: 'left', width: 160}
                ,{field: 'oldSpecCode', title: '规格码', fixed: 'left', width: 160}
                ,{field: 'warehouseName', title: '仓库'}
                ,{field: 'corporateName', title: '供应商'}
                ,{field: 'typeDesc',title: '库存变动方式', width: 120}
                ,{field: 'warehouseInventoryTypeName',title: '库存变动类型', width: 120}
                ,{field: 'quantityCurrent',title: '变动前包装库存', width: 130}
                ,{field: 'quantity',title: '变动包装数量', width: 120}
                ,{field: 'quantityAfter',title: '变动后包装库存', width: 130}
                ,{field: 'packName',title: '包装方案'}
                ,{field: 'quantityPackCurrent',title: '变动前基础库存', width: 130}
                ,{field: 'quantityPack',title: '变动基础数量', width: 120}
                ,{field: 'quantityPackAfter',title: '变动后基础库存', width: 130}
                ,{field: 'creatorName', title: '操作人'}
                ,{field: 'departmentName', title: '部门'}
                ,{field: 'createTime', title: '库存变动时间', width: 160}
                // ,{align: 'center', toolbar: '#inventoryProductRecordOutTableBar', title: '操作'}
            ]],
            done: function (res, curr, count) {

            }
        });

        // 渲染下拉框
       form.render('select');
        // 工具条点击事件
        table.on('tool(inventoryProductRecordOutTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'details') { //详情
                showEditModel(data);
            }
        });

        //监听排序
        table.on('sort(inventoryProductRecordOutTable)', function(obj){
            table.reload('inventoryProductRecordOutTable', {
                initSort: obj
                ,where: {
                    sort: obj.field
                    ,order: obj.type
                }
            });
        });

        // 搜索
        form.on('submit(formSubSearchLog)', function (data) {
            insTb.reload({where: data.field,page: {curr: 1}},'data');
        });

        // 重置搜索
        form.on('submit(resets)', function (data) {
            $(".lay-header").find('select').val("");
            $(".lay-header").find('input').val("");
            let field = data.field;
            for (let key in field) {
                field[key] = ''
            }
            insTb.reload({where: field, page: {curr: 1}}, 'data');
        });

        // 导出excel
        /*$('#inventoryProductRecordOutExport').click(function () {
            var checkRows = table.checkStatus('inventoryProductRecordOutTable');
            if (checkRows.data.length == 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                table.exportFile(insTb.config.id, checkRows.data, 'xls');
            }
        });*/

        // 导出
        $('#inventoryProductRecordOutExport').click(function () {
            var sTime = $('#sTime').val();
            var eTime = $('#eTime').val();
            if (sTime == "" && eTime == "") {
                layer.msg('请先选择创建时间！')
            } else {
                location.href = config.base + 'enterprisecab/order/admin/inventoryProductRecordOut/export?startCreateTime=' + sTime + '&endCreateTime=' + eTime;
            }
        });

        // 发送消息
        $("#sendMessage").click(function () {
            admin.req('user/admin/user/message/send', {mobile: "15307348603", agentid: "1000003"}, function (res) {
                console.log(res);
                if (res.errcode == "0") {
                    layer.msg('发送消息成功', {icon: 1, time: 1000});
                } else {
                    layer.msg('发送消息失败:'+res.errmsg, {icon: 2, time: 1000});
                }
            }, 'GET');
        })

    });
</script>